package com.prayerdiary.app;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.Toast;

import com.facebook.UiLifecycleHelper;
import com.facebook.widget.FacebookDialog;
import com.prayerdiary.fragments.AddMonthFragment;
import com.prayerdiary.fragments.AddQazaDateFragment;
import com.prayerdiary.utils.PDProgressDialog;
import com.prayerdiary.utils.Utils;

public class PDMainActivity extends FragmentActivity implements OnClickListener
{
	private UiLifecycleHelper uiHelper;

	@Override
	protected void onCreate(android.os.Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN, WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
		setContentView(R.layout.main_screen_layout);

		uiHelper = new UiLifecycleHelper(this, null);
		uiHelper.onCreate(savedInstanceState);

		addButtonListeners();
	};

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data)
	{
		super.onActivityResult(requestCode, resultCode, data);
		uiHelper.onActivityResult(requestCode, resultCode, data, new FacebookDialog.Callback()
		{
			@Override
			public void onError(FacebookDialog.PendingCall pendingCall, Exception error, Bundle data)
			{
				Log.e("Activity", String.format("Error: %s", error.toString()));
			}

			@Override
			public void onComplete(FacebookDialog.PendingCall pendingCall, Bundle data)
			{
				Log.i("Activity", "Success!");
			}

		});
	}

	@Override
	protected void onSaveInstanceState(Bundle outState)
	{
		super.onSaveInstanceState(outState);
		uiHelper.onSaveInstanceState(outState);
	}

	@Override
	public void onPause()
	{
		super.onPause();
		uiHelper.onPause();
	}

	@Override
	public void onDestroy()
	{
		super.onDestroy();
		uiHelper.onDestroy();
	}

	@Override
	protected void onRestart()
	{
		super.onRestart();
		uiHelper.onResume();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.main_menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
		// Handle item selection
		switch (item.getItemId())
		{
		case R.id.sharePD:
			shareDialogBuilder(PDMainActivity.this);
			return true;
		case R.id.exitPD:
			this.finish();
			System.exit(0);
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	@Override
	public void onClick(View v)
	{
		Fragment fr = null;
		View addQazaBtn = findViewById(R.id.tbm_add_qaza_date_btn);
		if (v == addQazaBtn)
		{
			v.setBackgroundResource(com.prayerdiary.app.R.drawable.add_qaza_tab_c);
			View btn = findViewById(R.id.tbm_add_month_btn);
			btn.setBackgroundResource(com.prayerdiary.app.R.drawable.add_del_month_tab);

			fr = new AddQazaDateFragment();

			android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
			fragmentTransaction.replace(R.id.main_fragment, fr);
			fragmentTransaction.addToBackStack(null);

			// Commit the transaction
			fragmentTransaction.commit();
		}
		else if (v == findViewById(R.id.tbm_show_qaza_btn))
		{
			Intent i = new Intent(PDMainActivity.this, OfferedQazaActivity.class);
			PDProgressDialog.getInstanse().showDialog(this);
			startActivity(i);
		}
		else if (v == findViewById(R.id.tbm_add_month_btn))
		{
			v.setBackgroundResource(com.prayerdiary.app.R.drawable.add_del_month_tab_c);
			addQazaBtn = findViewById(R.id.tbm_add_qaza_date_btn);
			addQazaBtn.setBackgroundResource(com.prayerdiary.app.R.drawable.add_qaza_tab);

			fr = new AddMonthFragment();

			android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
			fragmentTransaction.replace(R.id.main_fragment, fr);
			fragmentTransaction.addToBackStack(null);

			// Commit the transaction
			fragmentTransaction.commit();
		}
		else if (v == findViewById(R.id.tbm_show_currrent_btn))
		{
			Intent i = new Intent(PDMainActivity.this, CurrentPrayerRecord.class);
			PDProgressDialog.getInstanse().showDialog(this);
			startActivity(i);
		}
	}

	private void shareDialogBuilder(PDMainActivity pdMainActivity)
	{
		try
		{
			if (FacebookDialog.canPresentOpenGraphActionDialog(PDMainActivity.this, FacebookDialog.OpenGraphActionDialogFeature.OG_ACTION_DIALOG))
			{
				FacebookDialog shareDialog = new FacebookDialog.ShareDialogBuilder(PDMainActivity.this).setLink("https://www.facebook.com/prayerdiarykts")
						.setApplicationName("PrayerDiary").build();
				uiHelper.trackPendingDialogCall(shareDialog.present());
			}
			else
			{
				// TODO login to facebook through web and share
				Utils u = new Utils();
				u.showMessage("Sorry", "Unable to share without Facebook app", PDMainActivity.this).run();
			}
		}
		catch (Exception e)
		{
			// TODO catch the specific exception
			Toast.makeText(PDMainActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
		}
	}

	private void addButtonListeners()
	{
		Button addQazaBtn = (Button) findViewById(R.id.tbm_add_qaza_date_btn);
		addQazaBtn.setOnClickListener(this);

		Button addMonthBtn = (Button) findViewById(R.id.tbm_add_month_btn);
		addMonthBtn.setOnClickListener(this);

		Button showQazaPrayer = (Button) findViewById(R.id.tbm_show_qaza_btn);
		showQazaPrayer.setOnClickListener(this);

		Button showCurrentPrayer = (Button) findViewById(R.id.tbm_show_currrent_btn);
		showCurrentPrayer.setOnClickListener(this);
	}

}